import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: null,
    // user: 
    // {
    //   id: 2,
    //   uuid: "u1",
    //   nickName: "u1",
    //   password: "123",
    //   phone: "123",
    //   sex: null,
    //   age: 0,
    //   state: 1,
    //   identity: 2,
    //   avatar: null,
    //   intro: null,
    //   createTime: "2023-05-29T03:15:12.000+00:00"
    // },
    manger:null,
    // manger:{
    //   id:1,
    //   nickName:"lbw",
    //   phone:"666",
    //   password:"666",
    //   createTime:"2023-05-25 18:58:22"
    // },
    isLoggedIn:false
  },
  getters: {
    getUser(state) {
      const user = sessionStorage.getItem('user')
      return user ? JSON.parse(user) : state.user;
    },
    getManger(state){
      const manger = sessionStorage.getItem('manger')
      return manger ? JSON.parse(manger) : state.manger;
    },
    isLoggedIn(state){
      const isLoggedIn = sessionStorage.getItem('isLoggedIn')
      return isLoggedIn ? JSON.parse(isLoggedIn) : state.isLoggedIn;
    }
  },
  mutations: {
    setUser(state, user) {
      state.user = user;
      sessionStorage.setItem('user', JSON.stringify(user))
    },
    setManger(state, manger){
      state.manger = manger;
      sessionStorage.setItem('manger', JSON.stringify(manger))
    },
    setLoggedIn(state, value){
      state.isLoggedIn = value;
      sessionStorage.setItem('isLoggedIn', JSON.stringify(value))
    }
  },
  actions: {
    login(context, user,manger) {
      console.log('context :>> ', context);
      context.commit('setUser', user);
      context.commit('setManger', manger);
      context.commit('setLoggedIn', true);
    },
    logout(context){
      context.commit('setLoggedIn', false);
    }
  },
  modules: {
  }
})
